package controllers

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"strconv"
	"userCRUD/model"
	"userCRUD/services"
	"userCRUD/utils"
)

type userService struct {
}

var User = new(userService)

func (userService) Login(c *gin.Context) {
	// 1、校验参数
	u := new(model.UserDTOLogin)
	if err := c.ShouldBindJSON(u); err != nil {
		// 请求参数有误
		zap.L().Warn("[controller userController Login] login with invalid param ", zap.Error(err))
		ResponseError(c, ErrorInvalidParams)
		return
	}
	// 2、业务处理
	data, err := services.User.Login(u)
	if err != nil {
		zap.L().Error("[controller userController Login] login failed ", zap.String("Account", u.Account), zap.Error(err))
		if errors.Is(err, gorm.ErrRecordNotFound) {
			ResponseErrorWithMsg(c, ErrorInvalidParams, "用户名或密码错误！")
			return
		}
		ResponseError(c, ErrorServerBusy)
		return
	}
	// 3、返回响应
	ResponseOK(c, data)
}

func (userService) GetUserVOByUserId(c *gin.Context) {
	value := c.Query(utils.KeyUserId)
	if value == "" {
		zap.L().Warn("[controllers GetUserVOByUserId] query userId failed ")
		ResponseError(c, ErrorInvalidParams)
		return
	}
	userId, err := strconv.ParseInt(value, 10, 64)
	if err != nil {
		zap.L().Warn("[controllers GetUserVOByUserId] parse userId failed ", zap.Error(err))
		ResponseError(c, ErrorInvalidParams)
		return
	}
	data, err := services.User.GetUserVOByUserId(userId)
	if err != nil {
		zap.L().Warn("[controllers GetUserVOByUserId] get user vo by userId error ", zap.Error(err))
		ResponseError(c, ErrorServerBusy)
		return
	}
	ResponseOK(c, data)
}

func (userService) GetUserByUserId(c *gin.Context) {
	value := c.Query(utils.KeyUserId)
	if value == "" {
		zap.L().Warn("[controllers GetUserVOByUserId] query userId failed ")
		ResponseError(c, ErrorInvalidParams)
		return
	}
	userId, err := strconv.ParseInt(value, 10, 64)
	if err != nil {
		zap.L().Warn("[controllers GetUserByUserId] parse userId failed ", zap.Error(err))
		ResponseError(c, ErrorInvalidParams)
		return
	}
	data, err := services.User.GetUserByUserId(userId)
	if err != nil {
		zap.L().Warn("[controllers userService GetUserByUserId] get user by userId failed ", zap.Error(err))
		ResponseError(c, ErrorServerBusy)
		return
	}
	ResponseOK(c, data)
}

func (userService) AddUser(c *gin.Context) {
	// 1、参数校验
	u := new(model.UserDTOAdd)
	if err := c.ShouldBindJSON(u); err != nil {
		// 请求参数有误
		zap.L().Error("[services userService AddUser] add user with invalid param ", zap.Error(err))
		ResponseError(c, ErrorInvalidParams)
		return
	}
	// 2、业务处理
	if err := services.User.AddUser(u); err != nil {
		zap.L().Error("[services userService AddUser] add user failed ", zap.Error(err))
		if errors.Is(err, services.ErrorUserExist) {
			ResponseErrorWithMsg(c, ErrorInvalidParams, err.Error())
			return
		}
		ResponseError(c, ErrorServerBusy)
		return
	}
	// 3、返回响应
	ResponseOK(c, nil)
}

func (userService) UpdateUserByAdmin(c *gin.Context) {
	//userId 不能为0,否则无法读取UserId
	u := new(model.UserDTOUpdateByAdmin)
	err := c.ShouldBindJSON(u)
	if err != nil || u.Password != u.RePassword {
		zap.L().Warn("[controller userController UpdateUserByAdmin] update user by admin with invalid param ", zap.Error(err))
		ResponseError(c, ErrorInvalidParams)
		return
	}
	// 业务
	err = services.User.UpdateUserByAdmin(u)
	if err != nil {
		zap.L().Warn("[controller userController UpdateUserByAdmin] update user by admin failed ", zap.Error(err))
		if errors.Is(err, services.ErrorUserExist) {
			ResponseErrorWithMsg(c, ErrorInvalidParams, err.Error())
			return
		}
	}
	ResponseOK(c, nil)
}
